//
//  UILabel+NLCustomLab.m
//  soosoa
//
//  Created by yj_zhang on 2017/8/16.
//  Copyright © 2017年 liq. All rights reserved.
//

#import "UILabel+NLCustomLab.h"
#import <objc/runtime.h>
#import "JKCategories.h"

static const char * SELECTEDKEY = "selectedKey";
static const char * ABLEKEY = "ableKey";
@implementation UILabel (NLCustomLab)
-(instancetype)init{
    self = [super init];
    if (self) {
    }
    return self;
}

-(instancetype)initWithText:(NSString *)text andTextColor:(UIColor *)textColor andTextAlignment:(NSTextAlignment)textAlignment andFont:(UIFont *)font
{
    self = [super init];
    if (self) {
        self.text = text;
        self.textColor = textColor;
        self.textAlignment = textAlignment;
        self.font = font;
    }
    return  self;
}

-(instancetype)initWithAttributedTitle:(NSString *)attributedText andTextColor:(UIColor *)textColor andTextAlignment:(NSTextAlignment)textAlignment andFont:(UIFont *)font andLineSpacing:(CGFloat)lineSpacing andRange:(NSRange)range andRangeTextColor:(UIColor *)rangeTextColor andRangeFont:(UIFont *)rangeFont
{
    
    self = [super init];
    if (self) {
        self.textColor = textColor;
        self.font = font;
        self.numberOfLines = 0;
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.alignment = textAlignment;
        paragraphStyle.lineSpacing = lineSpacing;
        NSDictionary * attributes = @{NSFontAttributeName : rangeFont, NSParagraphStyleAttributeName : paragraphStyle,NSForegroundColorAttributeName:rangeTextColor};
        NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString alloc] initWithString:attributedText];
        [attributedTitle addAttributes:attributes range:range];
        
        self.attributedText = attributedTitle;
        
    }
    return  self;
}


- (void)setLineSpace:(CGFloat)lineSpace{

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpace;//,NSKernAttributeName:@0.6f 字间距
    NSDictionary * attributes = @{ NSParagraphStyleAttributeName : paragraphStyle,NSKernAttributeName:@0.2f};
    NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString alloc] initWithString:self.text];
    [attributedTitle addAttributes:attributes range:NSMakeRange(0, self.text.length)];
    
    self.attributedText = attributedTitle;
}



/*
 *计算文字尺寸
 @param text  需要计算尺寸的文字
 @param font  文字的字体
 @param maxSize  文字的最大尺寸
 */
+ (CGSize)getSizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *attrs = @{NSFontAttributeName: font};
    
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}
#pragma mark---选中状态
-(void)setSelected:(BOOL)selected
{
    if (selected) {
        self.textColor = [UIColor jk_colorWithHexString:@"#33A6FF"];
    }
    else{
        self.textColor = [UIColor jk_colorWithHexString:@"#000000"];
    }
    objc_setAssociatedObject(self, SELECTEDKEY, @(selected), OBJC_ASSOCIATION_ASSIGN);
}
-(BOOL)selected
{
    return objc_getAssociatedObject(self, SELECTEDKEY);
    
}
#pragma mark - 复制
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    return (action == @selector(copyText:));
}

- (void)setCopyAvaiable{
    [self setIsCopyAble:YES];
}

- (void)attachTapHandler {
    self.userInteractionEnabled = YES;
    UILongPressGestureRecognizer *g = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [self addGestureRecognizer:g];
}

//  处理手势相应事件
- (void)handleTap:(UIGestureRecognizer *)g {
    [self becomeFirstResponder];
    
    UIMenuController *menu=[UIMenuController sharedMenuController];
    UIMenuItem *item = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copyText:)];
    [menu setMenuItems:[NSArray arrayWithObject:item]];
    [menu setTargetRect:self.frame inView:self.superview];
    [menu setMenuVisible:YES animated:YES];
    
}
//  复制时执行的方法
- (void)copyText:(id)sender {
    //  通用的粘贴板
    UIPasteboard *pBoard = [UIPasteboard generalPasteboard];
    
    //  有些时候只想取UILabel的text中的一部分
    if (objc_getAssociatedObject(self, @"expectedText")) {
        pBoard.string = objc_getAssociatedObject(self, @"expectedText");
    } else {
        
        //  因为有时候 label 中设置的是attributedText
        //  而 UIPasteboard 的string只能接受 NSString 类型
        //  所以要做相应的判断
        if (self.text) {
            pBoard.string = self.text;
        } else {
            pBoard.string = self.attributedText.string;
        }
    }
}

- (BOOL)canBecomeFirstResponder {
    return [objc_getAssociatedObject(self, ABLEKEY) boolValue];
}

- (void)setIsCopyAble:(BOOL)copyAble {
    objc_setAssociatedObject(self, ABLEKEY, @(copyAble), OBJC_ASSOCIATION_RETAIN);
    [self attachTapHandler];
}

- (BOOL)isCopyAble {
    return [objc_getAssociatedObject(self, ABLEKEY) boolValue];
}
@end
